package cn.gson.crmdemo.model.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.AddressDao;
import cn.gson.crmdemo.model.dao.OrderDao;
import cn.gson.crmdemo.model.dao.OrderdetailedDao;
import cn.gson.crmdemo.model.mapper.OrderMapper;
import cn.gson.crmdemo.model.pojo.Address;
import cn.gson.crmdemo.model.vo.DeliverdetailedProductVo;
import cn.gson.crmdemo.model.vo.OrderAddressSalesopportunityVo;
import cn.gson.crmdemo.model.vo.OrderdetailedVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class OrderService {
	@Autowired
	OrderDao od;
	@Autowired
	OrderdetailedDao ord;
	@Autowired
	OrderMapper om;
	@Autowired
	AddressDao ad;
	
	/**
	 * 查询订单
	 * @return
	 */
	public JSONArray selectOrder() {
		return om.selctOrder();
	}
	
	/**
	 * 待处理订单
	 * @return
	 */
	public JSONArray selctOrderdcl() {
		return om.selctOrderdcl();
	}
	
	/**
	 * 新增/修改订单
	 * @param emp
	 */
	public void addContract(OrderAddressSalesopportunityVo o) {
		od.save(o);
	}	
	
	/**
	 *  id查询订单表
	 * @param a
	 * @return
	 */
	public JSONArray selectOrderID(int a) {
		return om.selectOrderID(a);
	}
	
	
	
	/**
	 * 删除订单表
	 * @param a
	 */
	public void deleteOrder(int a) {
		od.deleteById(a);
	}
	
	
	/**
	 * 新增/修改订单
	 * @param emp
	 */
	public void addAddress(Address a) {
		ad.save(a);
	}
	
	
	
	/**
	 * 查询最近订单id
	 * @return
	 */
	public int selectMaxOrderID() {
		return om.selectMaxOrderID();
	}
	
	
	/**
	 * 查询最近订单商品
	 * @return
	 */
	public JSONArray selectQuotationWhereOrder(int Oid) {
		return om.selectQuotationWhereOrder(Oid);
	}
	
	
	/**
	 * 添加订单祥单
	 * @param deliver
	 */
	public void addOrderdetailed(OrderdetailedVo orderdetailed) {
		ord.save(orderdetailed);
	}
	
	
	
	/**
	 * 查询最大地址id
	 * @return
	 */
	public JSONArray selectMaxAddressId() {
		return om.selectMaxAddressId();
	}
	
	
	/**
	 * 查询销售机会
	 * @return
	 */
	public JSONArray selectSalesnities() {
		return om.selectSalesnities();
	}
	
	
	/**
	 * 查询地址联系人
	 * @return
	 */
	public JSONArray selectAddressName() {
		return om.selectAddressName();
	}
	
	
	/**
	 * 通过id查询详细地址
	 * @param a
	 * @return
	 */
	public JSONArray selectAddressID(int a) {
		return om.selectAddressID(a);
	}
	
	
	/**
	 * 修改订单审核状态
	 * @param oId
	 * @param state
	 */
	public void updateOrderState(int oId,int state) {
		OrderAddressSalesopportunityVo order=od.findById(oId).get();
		order.setO_checkState(state);
		od.save(order);
	}
	
	
	/**
	 * 查询报价单
	 * @return
	 */
	public JSONArray selectQuotation() {
		return om.selectQuotation();
	}
	
	
	/**
	 * 根据客户查询报价单
	 * @return
	 */
	public JSONArray selectQuotationwhereCustomer(int a) {
		return om.selectQuotationwhereCustomer(a);
	}
	
	
	
	/**
	 * 修改订单发货状态
	 * @param dstate
	 */
	public void updateDstate(int dstate) {
		om.updateDstate(dstate);
	}
	
	
	
	/**
	 * 查询最大出库id
	 * @return
	 */
	public int selectMaxOutorderId() {
		return om.selectMaxOutorderId();
	}
	
	
	
	/**
	 * 修改订单发货状态为已退货3
	 * @param dstate
	 */
	public void updateReturnState(int dstate) {
		om.updateReturnState(dstate);
	}
	
	
	
	/**
	 * 修改订单发货状态为已收货5
	 * @param dstate
	 */
	public void updateGoodsOk(int dstate) {
		om.updateGoodsOk(dstate);
	}
	
	
	/**
	 * 查询最大退货单id
	 * @return
	 */
	public int selectMaxReturnId() {
		return om.selectMaxReturnId();
	}
	
	
	
	/**
	 * 查询退货单
	 * @param a
	 * @return
	 */
	public JSONArray selectMaxReturnIdaddDetailed(int a) {
		return om.selectMaxReturnIdaddDetailed(a);
	}
	
	
	/**
	 * 查询发货状态
	 * @param state
	 * @return
	 */
	public JSONArray selectDstate(int state) {
		return om.selectDstate(state);
	}
	
	
	/**
	 * 模糊查询订单
	 * @param companyName
	 * @return
	 */
	public JSONArray mhselectCompany(String companyName) {
		return om.mhselectCompany(companyName);
	}
	
	
	
	/**
	 * 生成订单随机数 时间+随机数
	 * @return
	 */
	public String  random() {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String newDate=sdf.format(new Date());
		String result="";
		Random random=new Random();
		for(int i=0;i<3;i++){
			result+=random.nextInt(10);
		}
		System.out.println("时间"+newDate+"随机数"+result);
		return newDate+result;
	}
}
